package com.hg.library.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Process;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by admin on 2017/3/9.
 */

public class BoBoView extends ImageView {
    private static final String TAG = "BoBoView";

    Bitmap bitmap;

    Bitmap colorBitmap1, colorBitmap2;

    int mWidth = -1, mHeight = -1;

    private int process = 0;

    private boolean isStart, isPause;

    private static int FPS = 0;

    public BoBoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    Paint paint = new Paint();
    int k;

    public int getProcess() {
        return process;
    }

    public void setProcess(int process) {
        this.process = process;
    }

    public void setProcess(int process, Bitmap bitmap){
        setBoboBitmap(bitmap);
        setProcess(process);
    }

    public void showBoBoView(Bitmap bm) {
        setBoboBitmap(bm);
        start();
        FPS += 32;
    }

    public void start() {
        if (isPause) isPause = false;
        if (isStart) return;

        isStart = true;
        isPause = false;
        invalidate();

    }

    public boolean isStart() {
        return isStart;
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    public void setStart(boolean start) {
        isStart = start;
    }

    public void setBoboBitmap(Bitmap bm) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();

        if (layoutParams != null) {
            int width = layoutParams.width;
            int height = layoutParams.height;

            if (bm != null && !bm.isRecycled()) {
                bitmap = Bitmap.createScaledBitmap(bm, width, height, true);
            } else {
                throw new NullPointerException("bitmap is not allowed null");
            }
        }
    }

    public Bitmap getBoboBitmap(){
        return bitmap;
    }

    public void pause() {
        isPause = true;
    }

    public void hideBoBoView() {
        stop();
        FPS -= 32;
    }

    public void stop() {
        isStart = false;
        isPause = false;
        invalidate();
    }

    public void swapTo(BoBoView boBoView){

        boolean isStart = boBoView.isStart;
        boolean isPause = boBoView.isPause;

        Bitmap boBoBitmap = boBoView.bitmap;
        boBoView.bitmap = bitmap;
        bitmap = boBoBitmap;

        if (isStart){
            start();
        }
        if (isPause){
            pause();
        }

        boolean isStart1 = this.isStart;
        boolean isPause1 = this.isPause;

        if (isStart1){
            boBoView.start();
        }

        if (isPause1){
            boBoView.pause();
        }

        boBoView.isStart = this.isStart;
        boBoView.isPause = this.isPause;

        this.isStart = isStart;
        this.isPause = isPause;
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!isStart) {


            if (colorBitmap1 != null) {
                colorBitmap1.recycle();
                colorBitmap1 = null;
            }

            if (colorBitmap2 != null) {
                colorBitmap2.recycle();
                colorBitmap2 = null;
            }

            mWidth = -1;
            mHeight = -1;
            return;
        }

        if (mWidth == -1 && mHeight == -1) {
            mWidth = getWidth();
            mHeight = getHeight();
            colorBitmap1 = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
            colorBitmap2 = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        }

        paint.setXfermode(null);

        if (bitmap == null || (bitmap != null && bitmap.isRecycled())) return;

        canvas.drawBitmap(bitmap, 0, 0, paint);
        paint.setColor(/*Color.argb((int) (255), 0, 255, 255)*/Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(6);
        paint.setAntiAlias(true);

        int dst1Pixels[] = new int[getWidth() * getHeight()];
        int dst2Pixels[] = new int[getWidth() * getHeight()];
        int srcPixels[] = new int[getWidth() * getHeight()];

        {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), paint, Canvas.ALL_SAVE_FLAG);

            canvas.drawRect(new RectF(0, 0, getWidth(), getHeight()), paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            k += 1;

            bitmap.getPixels(srcPixels, 0, getWidth(), 0, 0, getWidth(), getHeight());
            for (int i = 0; i < getWidth(); i++) {
                int V = i;
                int D = 7;

                if (isPause) {
                    D = 0;
                }

                float angle = (float) (Math.PI / getWidth() * 2);
                float angle1 = (float) (180 / Math.PI * k);
                float height = getHeight() - getHeight() / 100.0f * getProcess();
                float Y = (float) (D / 2.0f * Math.sin(angle * V / 1.5f + angle1) + height - D / 2f);
                float angle2 = (float) (180 / Math.PI * k * 1.2f);

                float Y1 = (float) (D * Math.sin(angle * V / 1.5f + angle2) + height);

                for (int j = 0; j < getHeight(); j++) {
                    int color = 0;
                    int srcPixelsColor = srcPixels[j * getWidth() + i];
                    int srcPixelsAlpha = Color.alpha(srcPixelsColor);

                    int A = 255 / 3;
                    if (j <= Y) {

                        if (srcPixelsAlpha >= 255 / 3) {
                            color = Color.argb(A, 0, 0, 255);
                        }

                    }
                    dst1Pixels[j * getWidth() + i] = color;

                    color = 0;
                    if (j <= Y1) {
                        if (srcPixelsAlpha >= 255 / 3) {
                            color = Color.argb(A, 0, 0, 255);
                        }
                    }
                    dst2Pixels[j * getWidth() + i] = color;
                }
            }
            colorBitmap1.setPixels(dst1Pixels, 0, mWidth, 0, 0, mWidth, mHeight);
            canvas.drawBitmap(colorBitmap1, 0, 0, paint);

            canvas.restoreToCount(layerId);
        }

        {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            paint.setXfermode(null);
            canvas.drawRect(new RectF(0, 0, getWidth(), getHeight()), paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

            colorBitmap2.setPixels(dst2Pixels, 0, mWidth, 0, 0, mWidth, mHeight);
            canvas.drawBitmap(colorBitmap2, 0, 0, paint);

            canvas.restoreToCount(layerId);
        }

        if (isStart && !isPause){
            postInvalidateDelayed(FPS);
        }
    }

}
